/*
 * This class is the server and socket main class
 * It was based on a socket tutorial from the Oracle website
 */ 

import java.net.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.*;


import com.google.gson.Gson;

public class OOserver {
    public static void main(String[] args) throws IOException {
    	
    	
    	boolean deviceConnected = false;
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4444);
            System.out.println(serverSocket.getLocalSocketAddress());
            System.out.println(serverSocket);
        } 
        catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        						}
        
        System.out.println("server socket active");
        
        Gson gson = new Gson();			
		msg msgMessage = new msg("Server Active");
		String json = gson.toJson(msgMessage);
		System.out.println(json);

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }
        
        System.out.println("client socket active");
       

        ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream()); //WRITES TO CLIENT STREAM
        ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
		
     //   String inputLine, outputLine;
       // outputLine = "";
//        KnockKnockProtocol kkp = new KnockKnockProtocol();
//
//        outputLine = kkp.processInput(null);
        //out.println(outputLine);

        while (in != null) {
        	msg message = null;
        	//msg sendMsg = new msg("hello client");
        	
        	String str = null;
        	
        	try {
				//message = (msg) in.readObject();
				//System.out.println(message.getString());
        		
        		
        		
        		str = (String) in.readObject();
        		
        		System.out.println(str);
        		
        		DataRequest req = gson.fromJson(str, DataRequest.class);
        		System.out.println(req);
        		
        		String send_data;
        		
        		int i = 0;
        		
        		//ResultSet rs = new jdbc().acquireCombined();
        		//ResultSet rs = new jdbc().acquireFilter(-13905, 6716569, 1);
        		
        		ResultSet rs = new jdbc().acquireFilter(req.x, req.y, req.zoom);
        		
        		//ResultSet rs = new jdbc().acquireFilter(-13905, 6713569, 1);
        		
        		try {
					while(rs.next()){
					out.writeObject(rs.getString(1));
					System.out.println(rs.getString(1));
					i++;
						
					}
					out.writeObject("done");
					System.out.println("done");
					
					
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        		
        		
        		message = gson.fromJson(str, msg.class);   
        		System.out.println(i);
        		System.out.println(message.getString());
														} 
        	
        	catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
														}
        	
        catch (EOFException e) {
			e.printStackTrace();
			break;
										}
        	
//             //outputLine = kkp.processInput(inputLine);
//        	outputLine = inputLine;
//             out.println(outputLine + " waste");
//             out.println(serverSocket.getLocalSocketAddress());
//             if (outputLine.equals("Bye."))
//                break;
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}
